package com.neuedu.his.service;

import com.alibaba.fastjson.JSON;
import com.neuedu.his.common.constants.HisConstans;
import com.neuedu.his.common.utils.ParamUtils;
import com.neuedu.his.dao.*;
import com.neuedu.his.entity.*;
import com.neuedu.his.vo.DoctorVO;

import javax.servlet.http.HttpServletRequest;
import java.net.URLDecoder;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class DoctorService {

    private RegisterDao registerDao = new RegisterDao();
    private CheckApplyDao checkApplyDao = new CheckApplyDao();
    private InspectApplyDao inspectApplyDao = new InspectApplyDao();
    private CheckitemDao checkitemDao = new CheckitemDao();
    private InspectitemDao inspectitemDao = new InspectitemDao();


    /**
     * 根据类型 查询，患者列表
     * @param request
     * @return
     */
    public List<Register> getRegistList(HttpServletRequest request) {

        //TODO  待处理
        Integer loginUserId = 2; //获取当前登录的身份
        Integer loginUserDeptId = 1; //获取当前登录的身医生所归属的科室



        String type = request.getParameter("type");
        if("personal".equals(type)){
            String  where  =  " where userid = ? ";
            return registerDao.selectList(where ,loginUserId);
        }else if("dept".equals(type)){

            String where  =  " where dept_id = ? ";
            return registerDao.selectList(where ,loginUserDeptId);
        }

        return  null;
    }

    public boolean receivePatient(HttpServletRequest request) {


        String registId = request.getParameter("registId");
        Register register = registerDao.selectOne(" where regist_id = ?", registId);

        //页面上传递的数据
        Register pageParamRegister = ParamUtils.getParam(Register.class,request);


        //覆盖更新的数据
        register.setReadme(pageParamRegister.getReadme());
        register.setPresent(pageParamRegister.getPresent());
        register.setPresentTreat(pageParamRegister.getPresentTreat());
        register.setHistory(pageParamRegister.getHistory());
        register.setAllergy(pageParamRegister.getAllergy());
        register.setDisease(pageParamRegister.getDisease());
        register.setSuit(pageParamRegister.getSuit());
        register.setDrug(pageParamRegister.getDrug());


        //修改患者状态变为 已接诊
        register.setStatus(HisConstans.REGISTER_DEAL);

        //更新
        return registerDao.updateById(register) >0;
    }

    public boolean saveCheckApply(HttpServletRequest request) {

        String urlParamstr = request.getParameter("paramstr");

        String json = URLDecoder.decode(urlParamstr);
        DoctorVO doctorVO = JSON.parseObject(json, DoctorVO.class);
        System.out.println("doctorVO.getRegistId() = " + doctorVO.getRegistId());
        System.out.println("doctorVO.getCheckItemIds = " + doctorVO.getCheckItemIds());

        Integer[] checkItemIds = doctorVO.getCheckItemIds();

        int successCount = 0;
        for (Integer checkItemId : checkItemIds) {
            Checkitem checkitem = checkitemDao.selectOne(" where check_id = ? ", checkItemId);

            CheckApply checkApply = new CheckApply();
            checkApply.setRegistId(doctorVO.getRegistId());//患者id
            checkApply.setCheckId(checkItemId);
            checkApply.setName(checkitem.getName());
            checkApply.setPrice(checkitem.getPrice());
            checkApply.setStatus(HisConstans.CHECK_APPLY_UNPAY); //待支付
            successCount += checkApplyDao.insert(checkApply);
        }

        return checkItemIds.length ==  successCount;
    }

    public Map queryApplyItem(HttpServletRequest request) {
        Map res = new HashMap();

        String registerId = request.getParameter("registerId");

        //根据患者 id， 查询已经申请的 检查、检验项目
        List<CheckApply> checkItems = checkApplyDao.selectList(" where regist_id = ? ", registerId);
        List<InspectApply>   inspectItems = inspectApplyDao.selectList(" where regist_id = ? ", registerId);

        res.put("checkItems",checkItems);
        res.put("inspectItems",inspectItems);

        return res;
    }

    public boolean saveInspectApply(HttpServletRequest request) {

        String urlParamstr = request.getParameter("paramstr");

        String json = URLDecoder.decode(urlParamstr);
        DoctorVO doctorVO = JSON.parseObject(json, DoctorVO.class);


        Integer[] inspectItemIds = doctorVO.getInspectItemIds();

        int successCount = 0;
        for (Integer inspectItemId : inspectItemIds) {
            Inspectitem inspectitem = inspectitemDao.selectOne(" where inspect_id = ? ", inspectItemId);

            InspectApply inspectApply= new InspectApply();
            inspectApply.setRegistId(doctorVO.getRegistId());//患者id
            inspectApply.setInspectId(inspectItemId);
            inspectApply.setName(inspectitem.getName());
            inspectApply.setPrice(inspectitem.getPrice());
            inspectApply.setStatus(HisConstans.CHECK_APPLY_UNPAY); //待支付
            successCount += inspectApplyDao.insert(inspectApply);
        }

        return inspectItemIds.length ==  successCount;
    }
}
